package com.ruoyi.shop.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.Cart;
import com.ruoyi.shop.service.ICartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cartController
 *
 * @author ruoyi
 * @date 2025-06-08
 */
@Anonymous
@RestController
@RequestMapping("/shop/cart")
public class CartController extends BaseController
{
    @Autowired
    private ICartService cartService;

    /**
     * 查询cart列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Cart cart)
    {
        startPage();
        List<Cart> list = cartService.selectCartList(cart);
        return getDataTable(list);
    }

    /**
     * 导出cart列表
     */
    @Log(title = "cart", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cart cart)
    {
        List<Cart> list = cartService.selectCartList(cart);
        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
        util.exportExcel(response, list, "cart数据");
    }

    /**
     * 获取cart详细信息
     */
    @GetMapping(value = "/{cartid}")
    public AjaxResult getInfo(@PathVariable("cartid") Long cartid)
    {
        return AjaxResult.success(cartService.selectCartByCartid(cartid));
    }

    /**
     * 新增cart
     */
    @PostMapping
    public AjaxResult add(@RequestBody Cart cart) {
        Cart c1 = new Cart();
        BeanUtils.copyProperties(cart,c1);

        Cart c = cart;
        c.setPrice(null);
        c.setQuantity(null);
        List<Cart> list = cartService.selectCartList(c);
        if (list.size() != 0) {
            cart.setUpdateDate(new Date());
            int i = cartService.updateCartNum(c1); //修改数量
            if (i > 0) {
                return AjaxResult.success("修改成功");  // 原ResponseResult.createBySuccess(200, "", "修改成功")
            } else {
                return AjaxResult.error("修改失败");    // 原ResponseResult.createByFail(500, "修改失败")
            }
        } else {
            int i = cartService.insertCart(c1);
            if (i > 0) {
                return AjaxResult.success("添加成功");  // 原ResponseResult.createBySuccess(200, "", "添加成功")
            } else {
                return AjaxResult.error("添加失败");     // 原ResponseResult.createByFail(500, "添加失败")
            }
        }
    }

    /**
     * 修改cart
     */
    @Log(title = "cart", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cart cart)
    {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除cart
     */
    @Log(title = "cart", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartids}")
    public AjaxResult remove(@PathVariable Long[] cartids)
    {
        return toAjax(cartService.deleteCartByCartids(cartids));
    }
}
